package com.PMSys.Service;


import java.util.Date;
import java.util.List;

import com.PMSys.Common.DbUtils;
import com.PMSys.Dao.DepartmentDao;
import com.PMSys.Dao.DepartmentDaoImp;
import com.PMSys.Dao.PostDao;
import com.PMSys.Dao.PostDaoImp;
import com.PMSys.Dao.ReportManagementDao;
import com.PMSys.Dao.ReportManagementDaoImpl;
import com.PMSys.Dao.StaffDao;
import com.PMSys.Dao.StaffDaoImp;
import com.PMSys.Demain.Department;
import com.PMSys.Demain.DepartmentalMob;
import com.PMSys.Demain.Dimission;
import com.PMSys.Demain.Post;
import com.PMSys.Demain.PostMob;
import com.PMSys.Demain.Staff;

public class ReportManagementService {

	private ReportManagementDao reportManagementDao;

	public ReportManagementService() {
		reportManagementDao = new ReportManagementDaoImpl();
	}

	public List<Staff> selectNewEmployess(Date startDate, Date endDate) {

		if (startDate.getTime() > endDate.getTime()) {
			return null;
		}
		
		List<Staff> newEmployesList = reportManagementDao.selectNewEmplloyess(startDate, endDate);

		DepartmentDao departmentDao = new DepartmentDaoImp();
		PostDao postDao = new PostDaoImp(DbUtils.getConnection());

		for (Staff staff : newEmployesList) {
			Department department = departmentDao.selectById(Integer.parseInt(staff.getD_id()));
			staff.setDepartment(department);
			
			Post temp = new Post();
			temp.setP_id(staff.getP_id());
			
			Post post = postDao.getPostId(temp);
			staff.setPost(post);
		}

		return newEmployesList;
	}

	public List<DepartmentalMob> selectDepartmentalMob(Date startDate, Date endDate) {

		if (startDate.getTime() > endDate.getTime()) {
			return null;
		}
		
		List<DepartmentalMob> departmentalMobList = reportManagementDao.selectDepartmentalMob(startDate, endDate);

		DepartmentDao departmentDao = new DepartmentDaoImp();
		StaffDao staffDao = new StaffDaoImp(DbUtils.getConnection());

		for (DepartmentalMob departmentalMob : departmentalMobList) {
			Department oldDepartment = departmentDao.selectById(Integer.parseInt(departmentalMob.getD_oldId()));
			Department newDepartment = departmentDao.selectById(Integer.parseInt(departmentalMob.getD_newId()));
			
			departmentalMob.setOldDepartment(oldDepartment);
			departmentalMob.setNewDepartment(newDepartment);
			
			Staff temp = new Staff();
			temp.setS_id(departmentalMob.getS_id());
			
			Staff staff = staffDao.getSelectIdStaff(temp);
			departmentalMob.setStaff(staff);
			
		}
		
		return departmentalMobList;
	}

	public List<PostMob> selectPostMob(Date startDate, Date endDate) {
		if (startDate.getTime() > endDate.getTime()) {
			return null;
		}
		List<PostMob> postMobList = reportManagementDao.selectPostMob(startDate, endDate);

		PostDao postDao = new PostDaoImp(DbUtils.getConnection());
		StaffDao staffDao = new StaffDaoImp(DbUtils.getConnection());

		for (PostMob postMob : postMobList) {

			Post oldPost = postDao.selectById(postMob.getP_oldId());
			Post newPost = postDao.selectById(postMob.getP_newId());
			
			postMob.setOldPost(oldPost);
			postMob.setNewPost(newPost);
			
			Staff temp = new Staff();
			temp.setS_id(postMob.getS_id());
			
			Staff staff = staffDao.getSelectIdStaff(temp);
			postMob.setStaff(staff);
			
		}

		return postMobList;
		
	}

	public List<Dimission> selectDimission(Date startDate, Date endDate) {
		if (startDate.getTime() > endDate.getTime()) {
			return null;
		}
		
		List<Dimission> dimissionList = reportManagementDao.selectDimission(startDate, endDate);

		StaffDao staffDao = new StaffDaoImp(DbUtils.getConnection());
		PostDao postDao = new PostDaoImp(DbUtils.getConnection());
		DepartmentDao departmentDao = new DepartmentDaoImp();
		for (Dimission dimission : dimissionList) {
			
			Staff temp = new Staff();
			temp.setS_id(dimission.getS_id());
			
			Staff staff = staffDao.getSelectIdStaff(temp);
			dimission.setStaff(staff);
			
			staff.setPost(postDao.selectById(staff.getP_id()));
			staff.setDepartment(departmentDao.selectById(Integer.parseInt(staff.getD_id())));
		}
		return dimissionList;
	}

}
